package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.base.rxbus.Subscribe;
import com.shd.edanpei.bean.CustomerBean;
import com.shd.edanpei.presenter.contract.CarNeedDataContract;
import com.shd.edanpei.presenter.CarNeedDataPresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/9/21.
 * 车辆订单 订单所需材料1
 */

@SuppressWarnings("ALL")
public class CarNeedDataActivity1 extends BaseActivity<CarNeedDataPresenter>
        implements CarNeedDataContract.CarNeedDataIView {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.et_carNo)
    EditText etCarNo;
    @BindView(R.id.tv_carDistance)
    TextView tvCarDistance;
    @BindView(R.id.tv_carAge)
    TextView tvCarAge;
    @BindView(R.id.tv_carStatus)
    TextView tvCarStatus;
    private int orderID;

    private int from = -1;


    @Override
    protected int setLayout() {
        return R.layout.activity_carneeddata1;
    }

    @Override
    protected CarNeedDataPresenter bindPresenter() {
        return new CarNeedDataPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        RxBus.get().register(this);
        headline.setText("订单所需材料");
        Bundle extras = getIntent().getExtras();
        from = extras.getInt(Constant.FROM);
        if (from == 1) {
            headline.setText("修改订单所需材料");
            orderID = extras.getInt(Constant.ORDER_ID);
            mPresenter.getOrderInfo(orderID);
        } else {
            CustomerBean customerBean = extras.getParcelable(Constant.DATA);
            mPresenter.setCustomerBean(customerBean);
        }
    }

    @Override
    protected void initListener() {
        etCarNo.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mPresenter.setCarNo(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @OnClick(R.id.iv_headline_back)
    void onBack(){
        finish();
    }

    @OnClick(R.id.rl_carDistance)
    void onCarDistance() {
        mPresenter.setCarDistance(tvCarDistance);
    }

    @OnClick(R.id.rl_carAge)
    void onCarAge() {
        mPresenter.setCarAge(tvCarAge);
    }

    @OnClick(R.id.rl_carStatus)
    void onCarStatus() {
        mPresenter.setCarStatus(tvCarStatus);
    }

    @OnClick(R.id.tv_next)
    void onNext() {
        mPresenter.next(from);
    }

    @Subscribe(code = RxBusCode.NEED_DATA_SUBMIT_SUCCESS)
    public void doneNeedData(CustomerBean bean) {
        finish();
    }

    @Override
    protected void onDestroy() {
        RxBus.get().unRegister(this);
        super.onDestroy();
    }

    @Override
    public void setText(TextView text, String str) {
        text.setText(str);
        text.setTextColor(getResources().getColor(R.color.black));
    }

    @Override
    public void setData(CustomerBean bean) {
        if (!TextUtils.isEmpty(bean.getCarNo()))setText(etCarNo, bean.getCarNo());
        if (!TextUtils.isEmpty(bean.getDrivingDistance()))setText(tvCarDistance, bean.getDrivingDistance());
        if (!TextUtils.isEmpty(bean.getCarAge()))setText(tvCarAge, bean.getCarAge());
       if (!TextUtils.isEmpty(bean.getCarStatus()))setText(tvCarStatus, bean.getCarStatus());
    }

}
